package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.AppBean;
import cst.a931xmc.yuejian20.Bean.AppnumUpdateBean;
import cst.a931xmc.yuejian20.Bean.TrainBean;
import cst.a931xmc.yuejian20.Bean.TrainOrderBean;
import cst.a931xmc.yuejian20.Interface.TrainIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.TrainService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TrainModel<T> extends ApiModel implements TrainIface {
    private Retrofit retrofit;
    private TrainService service;

    public TrainModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(TrainService.class);

    }

    @Override
    public void getList(int train_type, final Listener l) {
        Call<List<TrainBean>> call = service.getTrainList(train_type);
        call.enqueue(new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getDetail(int train_id, final Listener l) {
        Call<TrainBean> call = service.getTrainDetail(train_id);
        call.enqueue(new Callback<TrainBean>() {
            @Override
            public void onResponse(Call<TrainBean> call, Response<TrainBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<TrainBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void updateTrainAppNum(int train_id, int train_appnum) {
        Call<AppnumUpdateBean> call = service.updateTrainAppNum(train_id,train_appnum);
        call.enqueue(new Callback<AppnumUpdateBean>() {
            @Override
            public void onResponse(Call<AppnumUpdateBean> call, Response<AppnumUpdateBean> response) {
                response.body();
            }

            @Override
            public void onFailure(Call<AppnumUpdateBean> call, Throwable t) {
                t.toString();
            }
        });
    }

    public void order(int train_id, String train_name, String train_pic, String train_intro,
                      int user_id, String torder_rname, String torder_tel, String torder_idcard,
                      int torder_num, String torder_price, final Listener l){
        Call<AppBean> call = service.order(train_id,train_name,train_pic,train_intro,user_id,torder_rname,torder_tel,torder_idcard,torder_num,torder_price);
        call.enqueue(new Callback<AppBean>() {
            @Override
            public void onResponse(Call<AppBean> call, Response<AppBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<AppBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getOrderList(int user_id, final Listener l){
        Call<List<TrainOrderBean>> call = service.getTrainorderList(user_id);
        call.enqueue(new Callback<List<TrainOrderBean>>() {
            @Override
            public void onResponse(Call<List<TrainOrderBean>> call, Response<List<TrainOrderBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<TrainOrderBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getOrderDetail(int torder_id, final Listener l){
        Call<TrainOrderBean> call = service.getTrainorderDetail(torder_id);
        call.enqueue(new Callback<TrainOrderBean>() {
            @Override
            public void onResponse(Call<TrainOrderBean> call, Response<TrainOrderBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<TrainOrderBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
